package com.typhoon.spring_boot_demo1.config;

import com.typhoon.spring_boot_demo1.dao.BaseDao;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 数据源配置类
 *
 */
public class DataSourceConfig implements BeanPostProcessor {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if(bean instanceof BaseDao) {
            ((BaseDao)bean).setJdbcTemplate(jdbcTemplate);
            System.out.println(beanName + "完成设置jdbcTemplate");
        }
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        return bean;
    }
}
